package pnu.ssm.sbchild;

import pnu.ssm.koreanDictionary.KoreanDictionary;
import pnu.ssm.parser.KoreaDictionaryXMLParser;
import pnu.ssm.question.Question;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class ChoiceActivity extends Activity {
	public static final String TAG = "ChoiceActivity";

	public static final int KOREA_DICTIONARY = 0;
	
	public static final int CHOICE_ACTIVITY = 1;
	public static final int NONE = 2;
	public static final int SUCCESS = 3;
	public static final int FAIL = 4;

	public static final String QUESTION = "Question";

	private Question mQuestion;

	private AssetManager assets;

	private Button btn_banner;
	private TextView tv_question;
	private TextView[] tv_examples = new TextView[5];
	private Button[] btn_examples = new Button[5];

	private SharedPreferences pref;
	private SharedPreferences.Editor editor;
	
	private KoreanDictionary koreaDictionary;
	
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			switch(msg.what) {
				case KOREA_DICTIONARY:
					koreaDictionary = (KoreanDictionary)msg.obj;
//					btn_banner.setText(koreaDictionary.getTitle());
					
					break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_choice);

		preprocessing();
		
		setupGUI();
		
		doParsing();
		
		Log.d(TAG, "ok done.");
	}
	
	private void doParsing() {
//		Log.i(TAG, "doParsing");
//		
//		new AsyncTask<Void, Void, Void>(){
//			@Override
//			protected void onPreExecute() {
//				super.onPreExecute();
//				btn_banner.setText("처리중...");
//			};
//			@Override
//			protected Void doInBackground(Void... arg0) {
//				new Thread(new KoreaDictionaryXMLParser(mHandler)).start();
//				
//				return null;
//			}
//			@Override
//			protected void onPostExecute(Void result) {
//				super.onPostExecute(result);
//			};
//		}.execute(null,null,null);
	}
	
	private void preprocessing() {
		setResult(NONE);
		assets = getResources().getAssets();

		pref = getPreferences(MODE_PRIVATE);
		editor = pref.edit();
	}
	
	private void setupGUI() {
		Intent intent = getIntent();
		mQuestion = (Question)intent.getSerializableExtra(QUESTION);
		
		String question = mQuestion.getQuestion();
		String[] answerList = mQuestion.getArrAnswer();
		
		btn_banner = (Button) findViewById(R.id.btn_banner);
//		btn_banner.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				showDialog(true);
//			}
//		});
		
		tv_question = (TextView) findViewById(R.id.tv_question);
		tv_question.setText(question);

		int[] arrTextViews = new int[]
				{
				R.id.tv_example1,
				R.id.tv_example2,
				R.id.tv_example3,
				R.id.tv_example4,
				R.id.tv_example5 
				};
		
		int[] arrButtons = new int[]
				{
				R.id.btn_example1,
				R.id.btn_example2,
				R.id.btn_example3,
				R.id.btn_example4,
				R.id.btn_example5 
				};

		for (int i = 0; i < tv_examples.length; i++) {
			tv_examples[i] = (TextView) findViewById(arrTextViews[i]);
			tv_examples[i].setText(answerList[i]);
			
			btn_examples[i] = (Button) findViewById(arrButtons[i]);
			btn_examples[i].setOnClickListener(clickListener);
		}
	}

	/*public OnCheckedChangeListener listener = new OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(CompoundButton c, boolean isChecked) {

			int num = 0;
			int id = c.getId();

			for (int i = 0; i < cb_choices.length; i++) {
				if (id == cb_choices[i].getId())
					num = i;
			}

			if (mProblem.isCorrect(tv_answers[num].getText().toString())) {
				setResult(ChoiceActivity.SUCCESS);
			} else {
				setResult(ChoiceActivity.FAIL);
			}

			finish();
		}
	};*/

	private void showDialog(boolean isVisible){
		String msg = koreaDictionary.getContent();
		
	    AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
	    alt_bld.setMessage(msg)
	    .setCancelable(true)
	    .setPositiveButton("확인", new DialogInterface.OnClickListener() {
	        @Override
			public void onClick(DialogInterface dialog, int id) {
	        	doParsing();
	        }});
	    AlertDialog alert = alt_bld.create();
	    alert.setTitle(koreaDictionary.getTitle());
	    alert.setIcon(R.drawable.ic_launcher);
	    alert.show();
	}
	
	/*public Bitmap loadBitmap(String path, int count) {
		String bitmapPath = "level8/" + path + "/";
		String filePath = bitmapPath + count + ".bmp";
		Bitmap bitmap = null;
		try {
			bitmap = BitmapFactory.decodeStream(assets.open(filePath));
		} catch (IOException e) {
			e.printStackTrace();
		}
		if(bitmap == null) {
			Log.e(TAG, "fail to load bitmap. path error");
			return bitmap;
		}
		int viewWidth = ChineseHelper.DEVICE_WIDTH;
		float width = bitmap.getWidth();
		float height = bitmap.getHeight();

		float percente = width / 100;
		float scale = viewWidth / percente;
		width *= (scale / 100);
		height *= (scale / 100);

		Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap,
				(int) width, (int) height, false);

		return scaledBitmap;
	}*/
	
	private OnClickListener clickListener  = new OnClickListener() {
		@Override
		public void onClick(View v) {
			int num = Integer.parseInt(((Button)v).getText().toString());
			
			Log.i(TAG, ((Button)v).getText() + " " + num + "");
			
			if (mQuestion.isCorrect(num)) {
				setResult(ChoiceActivity.SUCCESS);
			} else {
				setResult(ChoiceActivity.FAIL);
			}
			
			//setResult(ChoiceActivity.SUCCESS);
			finish();
		}
	};

	/*private OnTouchListener touchListener_question = new OnTouchListener() {
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			int eventAction = event.getAction();
			
			RelativeLayout lo_question = (RelativeLayout)findViewById(R.id.lo_question);
			RelativeLayout lo_setAnswer = (RelativeLayout)findViewById(R.id.lo_setAnswer);

			if (eventAction == MotionEvent.ACTION_DOWN) {
				lo_question.setVisibility(LinearLayout.INVISIBLE);
				lo_setAnswer.setVisibility(LinearLayout.VISIBLE);

			} else if (eventAction == MotionEvent.ACTION_UP) {
				lo_question.setVisibility(LinearLayout.VISIBLE);
				lo_setAnswer.setVisibility(LinearLayout.INVISIBLE);
			}
			
			return false;
		}
	};

	private OnTouchListener touchListener_setAnswer = new OnTouchListener() {
		private final int START_DRAG = 0;
		private final int END_DRAG = 1;
		private int isMoving;
		private int oldX, oldY;
		private boolean start_yn = true;

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			int eventAction = event.getAction();

			if (eventAction == MotionEvent.ACTION_DOWN) {
				if (start_yn) {
					oldX = (int) event.getX();
					oldY = (int) event.getY();
					start_yn = false;
				}
				isMoving = START_DRAG;
			} else if (eventAction == MotionEvent.ACTION_UP) {
				isMoving = END_DRAG;
			} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
				int newX = (int) event.getX();
				int newY = (int) event.getY();

				int dx = oldX - newX;
				int dy = oldY - newY;

				if (isMoving == START_DRAG) {
					v.setX(dx);
					v.setY(dy);
				}
			}
			
			return false;
		}
	};*/
}
